package com.aiit.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component//不要忘记把处理器加到IOC容器中
public class MyHandler implements MetaObjectHandler {
    private final static Date DATE_TIME = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String str = dateFormat.format(DATE_TIME);
    Date date = dateFormat.parse(str);

    public MyHandler() throws ParseException {
    }

    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        System.out.println("Date:"+date);
        //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject)
        this.setFieldValByName("createTime", date,metaObject);
        this.setFieldValByName("updateTime", date,metaObject);
    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill.....");
        System.out.println("Date:"+date);
        this.setFieldValByName("updateTime",date,metaObject);
    }
}
